/**
 * PDF 工具函数
 */

/**
 * 在新窗口中预览 PDF
 * @param base64 PDF 的 base64 编码
 * @param title 窗口标题
 */
export const previewPDFInNewWindow = (base64: string, title = 'PDF 预览') => {
  const url = `data:application/pdf;base64,${base64}`
  // 创建预览窗口
  const previewWindow = window.open('', '_blank', 'width=1200,height=800,scrollbars=no,resizable=no')
  if (previewWindow) {
    previewWindow.document.write(`
        <!DOCTYPE html>
        <html>
        <head>
          <title>${title}</title>
          <style>
            body { margin: 0; padding: 0; }
            iframe { width: 100%; height: 99vh; border: none;overflow: hidden; }
          </style>
        </head>
        <body>
          <iframe src="${url}" type="application/pdf"></iframe>
        </body>
        </html>
      `)
    previewWindow.document.close()
  }
}

/**
 * 检查浏览器是否支持 PDF 预览
 * @returns 是否支持
 */
export const isPDFSupported = (): boolean => {
  // 检查浏览器是否支持 PDF 预览
  const userAgent = navigator.userAgent.toLowerCase()
  const isChrome = userAgent.includes('chrome')
  const isFirefox = userAgent.includes('firefox')
  const isSafari = userAgent.includes('safari') && !userAgent.includes('chrome')
  const isEdge = userAgent.includes('edge')

  // 现代浏览器通常支持 PDF 预览
  return isChrome || isFirefox || isSafari || isEdge
}

/**
 * 直接打印 PDF（不预览）
 * @param base64 PDF 的 base64 编码
 */
export const printPDFDirectly = (base64: string) => {
  const url = `data:application/pdf;base64,${base64}`
  const printWindow = window.open(url, '_blank')
  if (printWindow) {
    printWindow.onload = () => {
      printWindow.print()
      // 打印完成后关闭窗口
      setTimeout(() => {
        printWindow.close()
      }, 1000)
    }
  }
}

/**
 * 在新窗口中预览富文本内容
 * @param htmlContent 富文本的 HTML 内容
 * @param title 窗口标题
 */
export const previewRichTextInNewWindow = (htmlContent: string, title = '富文本预览') => {
  // 创建预览窗口
  const previewWindow = window.open('', '_blank', 'width=1200,height=800,scrollbars=yes,resizable=yes')
  if (previewWindow) {
    previewWindow.document.write(`
        <!DOCTYPE html>
        <html>
        <head>
          <title>${title}</title>
          <style>
            body { 
              margin: 0; 
              padding: 0; 
              font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
              line-height: 1.6;
              color: #333;
              overflow: hidden;
            }
            iframe {
              width: 100vw;
              height: 100vh;
              border: none;
              display: block;
            }
            .content {
              max-width: 100%;
              background: white;
              padding: 20px;
              border-radius: 8px;
              box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            }
            img {
              max-width: 100%;
              height: auto;
            }
            table {
              border-collapse: collapse;
              width: 100%;
              margin: 10px 0;
            }
            table, th, td {
              border: 1px solid #ddd;
            }
            th, td {
              padding: 8px;
              text-align: left;
            }
            th {
              background-color: #f2f2f2;
            }
          </style>
        </head>
        <body>
          <iframe src="${htmlContent}"></iframe>
        </body>
        </html>
      `)
    previewWindow.document.close()
  }
}
